<?php

/**
 * This is the model class for table "{{machines}}".
 *
 * The followings are the available columns in table '{{machines}}':
 * @property integer $id
 * @property string $hostname
 * @property string $aliasname
 * @property string $ip
 * @property string $remark
 * @property integer $status
 * @property integer $flag
 * @property integer $conn
 * @property integer $workgroup
 * @property string $director
 * @property string $system
 * @property string $cpu
 * @property string $mem
 * @property string $disk
 * @property string $network
 * @property string $os
 * @property integer $isentity
 * @property integer $handle
 * @property integer $available
 */
class Machine extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Machine the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{machines}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('hostname, status, flag', 'required'),
			array('status, flag, conn, workgroup, isentity, handle', 'numerical', 'integerOnly'=>true),
			array('hostname', 'length', 'max'=>100),
			array('aliasname, disk, network, os', 'length', 'max'=>20),
			array('ip', 'length', 'max'=>50),
			array('remark', 'length', 'max'=>500),
			array('director', 'length', 'max'=>30),
			array('system', 'length', 'max'=>1000),
			array('cpu, mem', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, hostname, aliasname, ip, remark, status, flag, conn, workgroup, director, system, cpu, mem, disk, network, os, isentity, handle', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		//return array(
        //    'workgroup'=>array(self::MANY_MANY, 'workgroup', 'id'),
		//);
        return array(
        );
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'hostname' => 'Hostname',
			'aliasname' => 'Aliasname',
			'ip' => 'Ip',
			'remark' => '备注',
			'status' => 'Status',
			'flag' => '用途',
			'conn' => 'Alive',
			'workgroup' => '组',
			'director' => '负责人',
			'system' => 'System',
			'cpu' => 'Cpu',
			'mem' => 'Mem',
			'disk' => 'Disk',
			'network' => 'Network',
			'os' => 'Os',
			'isentity' => '类型',
			'handle' => '处理',
		    'available' => '是否可借',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
        //custom workgroup
        if(isset($this->workgroup) && $this->workgroup!='' && $this->workgroup != Null)
        {
            $workgroup = CActiveRecord::model('Workgroup')->find('groupname=\''.$this->workgroup.'\'');
            if($workgroup != Null)
                $this->workgroup = $workgroup->id;
        }

        //custom handle
        if(isset($this->handle) && $this->handle!='' && $this->handle != Null)
        {
            $handle = CActiveRecord::model('Handle')->find('handlename=\''.$this->handle.'\'');
            if($handle != Null)
                $this->handle = $handle->id;
        }

		$criteria->compare('id',$this->id);
		$criteria->compare('hostname',$this->hostname,true);
		$criteria->compare('aliasname',$this->aliasname,true);
		$criteria->compare('ip',$this->ip,true);
		$criteria->compare('remark',$this->remark,true);
		//$criteria->compare('status',$this->status);
		$criteria->compare('status',0);
		$criteria->compare('flag',$this->flag);
		$criteria->compare('conn',$this->conn);
		$criteria->compare('workgroup',$this->workgroup);
		$criteria->compare('director',$this->director,true);
		$criteria->compare('system',$this->system,true);
		$criteria->compare('cpu',$this->cpu,true);
		$criteria->compare('mem',$this->mem,true);
		$criteria->compare('disk',$this->disk,true);
		$criteria->compare('network',$this->network,true);
		$criteria->compare('os',$this->os,true);
		$criteria->compare('isentity',$this->isentity);
		//$criteria->compare('handle',$this->handle);
		$criteria->compare('handle',1);
		$criteria->compare('available',$this->available);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}
